package com.wanxi.education.mapper;

import com.wanxi.education.model.pojo.Catalogue;
import com.wanxi.education.model.pojo.Course;
import com.wanxi.education.model.vo.catalogue.*;
import com.wanxi.education.model.vo.course.*;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: WangRui
 * @Description:
 * @Date: Created in 2021/9/27 0027
 * @Version: 1.0
 */
public interface CourseMapper {

    void addCourseBasicInfo(Course course);

    void addCatalogue(Catalogue catalogue);

    void addChildCatalogues(List<CatalogueChildInfoAddVo> catalogueList);

    void addCatalogueVideoRelations(List<CatalogueChildInfoAddVo> catalogueList);

    @Update("update course set status = #{status},update_time = NOW() where id = #{id}")
    void updateStatusById(CourseStatusVo updateStatusById);

    void updateBasicById(CourseBasicVo courseBasicVo);

    @Update("update course set introduce = #{introduce} ,update_time = NOW() where id = #{id}")
    void updateInfoById(CourseInfoVo courseInfoVo);

    @Update("update catalogue set title = #{title},update_time = NOW() where id = #{id}")
    void updateCatalogueById(CatalogueUpdateVo catalogueUpdateVo);

    @Update("update video_directory_relation set video_id = #{videoId},update_time = NOW() where catalogue_id = #{id}")
    void updateCatalogueAndVideoById(CatalogueUpdateVo catalogueUpdateVo);

    List<CourseVo> findAll(CoursePageVo coursePageVo);

    @Update("update course set delete_status = 1,update_time = NOW() where id = #{id}")
    void deleteById(Long id);

    @Select("select c.id,c.title ,v.id video_id  from catalogue c left join video_directory_relation v on c.id = v.catalogue_id where c.id = #{id}")
    CatalogueInfoVo findCatalogueById(Long id);

    List<CatalogueAllVo> findCatalogueAllById(Long id);

    @Select("select id, introduce from course where id = #{id}")
    CourseInfoVo findCourseInfoById(Long id);


    @Select("select c1.id,title,picture,price,teacher,admin_id,weight,c2.ccie_id from course c1 LEFT JOIN ccie_course_relation c2 on c1.id = c2.course_id where c1.id = #{id}")
    CourseBasicVo findCourseBasicById(Long id);

    @Update("UPDATE catalogue c1 JOIN catalogue c2 ON (c1.id =#{moveDownId} AND c2.id = #{moveUpId})" +
            " SET c1.`order` = c2.`order`,c2.`order`=c1.`order`,c1.update_time = NOW(), c2.update_time = NOW()" +
            " where c1.pid = c2.pid;")
    boolean updateCatalogueOrderById(CatalogueOrderVo catalogueOrderVo);

    @Delete("delete from catalogue where id = #{id} or pid = #{id} ")
    void deleteCatalogueById(Long id);

    @Insert("insert recommend_course_relation (course_id,`order`,create_time,update_time) value(#{id},#{order},NOW(),NOW())")
    boolean addRecommend(Long id,Integer order);

    @Update("UPDATE recommend_course_relation c1 JOIN recommend_course_relation c2 ON (c1.course_id =#{moveDownId} AND c2.course_id = #{moveUpId})" +
            " SET c1.`order` = c2.`order`,c2.`order`=c1.`order`,c1.update_time = NOW(), c2.update_time = NOW();")
    boolean updateRecommendOrderById(CourseOrderVo courseOrderVo);

    @Select("select count(*) from recommend_course_relation")
    int countRecommend();

    @Select("select max(`order`) from recommend_course_relation")
    int findRecommendMaxOrder();

    @Select("select c2.title,c2.id from recommend_course_relation c1 LEFT JOIN course c2 on c1.course_id = c2.id ORDER BY c1.`order`")
    List<CourseRecommendVo> findRecommends();

    @Delete("delete from recommend_course_relation where course_id = #{id}")
    void deleteRecommend(Long id);

    @Insert("insert course_info_relation (course_id) value(#{id})")
    void addSaleAndPoint(Long id);


    @Update("UPDATE ccie_course_relation SET ccie_id = #{ccieId} where course_id = #{id}")
    void updateCertificate(Long id, Long ccieId);

    void addCatalogueById(CatalogueChildInfoAddVo catalogueChildInfoAddVo);


    @Insert("insert video_directory_relation (catalogue_id,video_id,admin_id,create_time,update_time) value(#{id},#{videoId},#{adminId},NOW(),NOW())")
    void addCatalogueVideoRelation(CatalogueChildInfoAddVo catalogueChildInfoAddVo);

    @Select("select max(`order`) from catalogue where pid = #{pid}")
    int findCatalogueMaxOrder(Long pid);

    @Select("select count(*) from catalogue where course_id = #{id} and pid != 0")
    Integer findCourseCountById(Long id);

    @Select("select price from course where id = #{id}")
    BigDecimal findCoursePriceById(Long id);

    @Insert("insert ccie_course_relation (course_id,ccie_id) value(#{id},#{ccieId})")
    void addCourseAndCcie(Long id, Long ccieId);

    @Select("select sale from course_info_relation where course_id =#{id}")
    Integer findCourseSaleById(Long id);
}
